home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / pgmbinomial / pgmbinomial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-29  |  3.1 KB  |  154 lines

  1.  
  2. /************************************************************************/
  3. #define OP_NAME      "pgmbinomial"
  4. #define VERSION      "1.06"
  5. #define DATE         "29.01.98"
  6. #define AUTHOR       "Stefan Diener"
  7. /************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <sys/types.h>
  15.  
  16. #include <STIMP/pgm.c>
  17.  
  18. struct PGM_Info source, desti;
  19. static int m, n;
  20. static int edge=1;
  21.  
  22. void Do_It(void)
  23. {
  24.   unsigned char breite, mitte;
  25.   int i, j, k, l, index;
  26.   unsigned int h[25], su, factor;
  27.   unsigned char *src, *dst;
  28.  
  29.   src=source.Data;
  30.   dst=desti.Data;
  31.   desti.maxval=source.maxval;
  32.  
  33.   /* Filtermaske initialisieren */
  34.   if (edge==1)
  35.   {
  36.     factor=16;
  37.     breite=3;
  38.     mitte=4;
  39.     h[0]=1;   h[1]=2;   h[2]=1;
  40.     h[3]=2;   h[4]=4;   h[5]=2;
  41.     h[6]=1;   h[7]=2;   h[8]=1;
  42.   }
  43.   else
  44.   {
  45.     factor=176;
  46.     breite=5;
  47.     mitte=12;
  48.     h[0]=1;     h[1]=4;      h[2]=6;       h[3]=4;      h[4]=1;
  49.     h[5]=4;     h[6]=8;      h[7]=14;     h[8]=8;      h[9]=4;
  50.     h[10]=6;   h[11]=14;   h[12]=28;   h[13]=14;   h[14]=6;
  51.     h[15]=4;   h[16]=8;     h[17]=14;   h[18]=8;    h[19]=4;
  52.     h[20]=1;   h[21]=4;     h[22]=6;     h[23]=4;    h[24]=1;
  53.   }
  54.  
  55.   /* Filtermaske anwenden */
  56.   for (i=edge;i<(n-edge);i++)
  57.     for (l=edge;l<(m-edge);l++)
  58.     {
  59.       index=l*n+i;
  60.       su=0;
  61.  
  62.       for (j=-edge; j<=edge; j++)
  63.         for (k=-edge; k<=edge; k++)
  64.           su+=h[j*breite+k+mitte]*src[index+j*n+k];
  65.  
  66.       su=su/factor;
  67.       dst[(l-edge)*(n-2*edge)+(i-edge)]=(su<desti.maxval) ? su : desti.maxval;
  68.     }
  69. }
  70.  
  71. int main(int argc,char **argv)
  72. /* Hauptprogramm */
  73. {
  74.   int i;
  75.  
  76.   /* offizielle Begruessung */
  77.   PrintOpening(argc, argv);
  78.  
  79.   /* Uebergebene Parameter auswerten */
  80.   for (i=1; i<argc; i++)
  81.   {
  82.     if ((argv[i][0]=='-') && argv[i][1])
  83.     {
  84.       switch (argv[i][1])
  85.       {
  86.         case '3': edge=1;
  87.                       break;
  88.  
  89.         case '5': edge=2;
  90.                       break;
  91.  
  92.         case 'v': beVerbose=FALSE;
  93.                       break;
  94.  
  95.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  96.                      Hilfe();
  97.                      exit(-1);
  98.                      break;
  99.       }
  100.     }
  101.  
  102.     if (argv[i][0]=='+')
  103.     {
  104.       switch (argv[i][1])
  105.       {
  106.         case 'v': beVerbose=TRUE;
  107.                       break;
  108.  
  109.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  110.                      Hilfe();
  111.                      exit(-1);
  112.                      break;
  113.       }
  114.     }
  115.   }
  116.  
  117.   /* Mindestzahl der Argumente pruefen */
  118.   if (argc<3)
  119.   {
  120.     PrintMessage("Wrong number of arguments !");
  121.     Hilfe();
  122.     exit(-1);
  123.   }
  124.  
  125.   /* Anzahl der Dateinamen überprüfen */
  126.   if (FilenameCount(argc, argv)!=2)
  127.   {
  128.     PrintMessage("Wrong number of file names !");
  129.     Hilfe();
  130.     exit(-1);
  131.   }
  132.  
  133.   if (ReadPGMFile(GetFilename(1,argc,argv), &source)==0)
  134.   {
  135.     m=source.height;
  136.     n=source.width;
  137.  
  138.     if (CreatePGMArray(m-2*edge, n-2*edge, &desti)==0)
  139.     {
  140.       PrintMessage("Working ...");
  141.       Do_It();
  142.  
  143.       WritePGMFile(GetFilename(2,argc,argv), &desti);
  144.       FreePGMArray(&desti);
  145.     }
  146.  
  147.     FreePGMArray(&source);
  148.   }
  149.  
  150.   PrintClosing();
  151.   exit(0);
  152. }
  153.  
  154.